# 导入模块
import requests
import redis
import time

# 设置时间格式
current_time = int(time.time() * 1000)
# 链接redis数据库
client = redis.StrictRedis(db=1, password='123456')

# 网址
for i in range(1, 283):
    print('开始读取第', i, '页')
    url = f'https://43.push2.eastmoney.com/api/qt/clist/get?pn={i}&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_={current_time}'
    # 链接网址
    response = requests.get(url)
    # 获取数据
    datas = response.json()["data"]["diff"]

    for data in datas:
        l = []
        print(f"当前读取的是{data['f14']}")
        d = {
            "代码": data['f12'],
            "股票名": data['f14'],
            "最新价": data['f3'],
            "最低价": data['f17']
        }
        l.append(d)
        str0 = str(l)
        time.sleep(0.5)
        # 添加数据到redis
        client.sadd('dfcf', str0)
        print(data['f14'], '读取成功')
        print()
        # 获取数据
        # r = client.smembers('dfcf')
        # print('dfcf', r)
    print('第', i, '读取成功')
# 释放资源
client.close()
